package com.bw.liuyushan.liuyushan20210929.MVP.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bw.liuyushan.liuyushan20210929.Adapter.FragmentMainAdapter;
import com.bw.liuyushan.liuyushan20210929.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.liuyushan20210929.MVP.Fragment.HomeFragment;
import com.bw.liuyushan.liuyushan20210929.MVP.Fragment.ListFragment;
import com.bw.liuyushan.liuyushan20210929.MVP.Fragment.MineFragment;
import com.bw.liuyushan.liuyushan20210929.MVP.Fragment.ShopFragment;
import com.bw.liuyushan.liuyushan20210929.MVP.Model.BaseModelTest;
import com.bw.liuyushan.liuyushan20210929.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.liuyushan20210929.R;
import com.bw.liuyushan.mvp_lib.View.BaseActivity;

import java.util.ArrayList;

public class MainActivity extends BaseActivity<BasePresenterTest> implements IContrackTest.C_ViewTest {


    private androidx.viewpager.widget.ViewPager vpMain;
    private android.widget.RadioGroup rgMain;
    private android.widget.RadioButton btnHome;
    private android.widget.RadioButton btnList;
    private android.widget.RadioButton btnShop;
    private android.widget.RadioButton btnMine;
    ArrayList<Fragment> fragments;


    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        //链接 防内存泄露
        mPresenter=new BasePresenterTest(new BaseModelTest(),this);

        vpMain = (ViewPager) findViewById(R.id.vp_Main);
        rgMain = (RadioGroup) findViewById(R.id.rg_Main);
        btnHome = (RadioButton) findViewById(R.id.btn_home);
        btnList = (RadioButton) findViewById(R.id.btn_list);
        btnShop = (RadioButton) findViewById(R.id.btn_shop);
        btnMine = (RadioButton) findViewById(R.id.btn_mine);
    }

    @Override
    public void initDate() {
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new ListFragment());
        fragments.add(new ShopFragment());
        fragments.add(new MineFragment());

        FragmentMainAdapter fragmentMainAdapter = new FragmentMainAdapter(getSupportFragmentManager(), fragments);
        vpMain.setAdapter(fragmentMainAdapter);

        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.btn_home:
                        vpMain.setCurrentItem(0);
                        break;
                    case R.id.btn_list:
                        vpMain.setCurrentItem(1);
                        break;
                    case R.id.btn_shop:
                        vpMain.setCurrentItem(2);
                        break;
                    case R.id.btn_mine:
                        vpMain.setCurrentItem(3);
                        break;
                }
            }
        });
        vpMain.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                    switch (position){
                        case 0:
                            rgMain.check(R.id.btn_home);
                            break;
                        case 1:
                            rgMain.check(R.id.btn_list);
                            break;
                        case 2:
                            rgMain.check(R.id.btn_shop);
                            break;
                        case 3:
                            rgMain.check(R.id.btn_mine);
                            break;
                    }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
